package com.example.pwj.ffstudy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.blankj.utilcode.util.LogUtils;
import com.example.pwj.ffstudy.R;
import com.example.pwj.ffstudy.base.BaseActivity;
import com.example.pwj.ffstudy.ui.fragment.FmManager;

import java.util.List;

/**
 * Created by pwj on 2017/8/24.
 */

public class ContainerActivity extends BaseActivity {
    @Override
    protected void init() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra(FmManager.BUNDLE_DATA);
        String fragmentName = bundle.getString(FmManager.FRAGMENT_NAME);
        Fragment fragment = FmManager.getFragmentByName(fragmentName);
        fragment.setArguments(bundle);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.llContainer, fragment);
        transaction.commit();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void back() {
        FragmentManager fm = getSupportFragmentManager();
        if (fm.popBackStackImmediate()) {
        } else {
            finish();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_container;
    }

}
